package com.JobFind.DAO.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.JobFind.DAO.ILogDAO;
import com.JobFind.model.TbLog;

@Component
public class LogDAO implements ILogDAO {
	private SessionFactory sessionFactory;

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public Integer addLog(TbLog logBean) throws Exception {
		Integer result = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			result = (Integer) session.save(logBean);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<TbLog> getLogInfo(String startTime, String endTime)
			throws Exception {
		List<TbLog> result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			startTime+=" 00:00:00";
			endTime+=" 23:59:59";
			String hql = "from TbLog where logTime>=? and logTime<=? order by logTime desc";
			Query query = session.createQuery(hql);
			query.setString(0,startTime);
			query.setString(1,endTime);
			result = query.list();
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
		}
		return result;
	}

}
